package com.parking.platform.rabbit;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.parking.common.core.model.RedisKey;
import com.parking.common.core.model.StateData;
import com.parking.common.web.util.RedisUtil;
import com.parking.platform.dao.*;
import com.parking.platform.entity.*;
import javafx.application.Platform;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Component
public class PlatformConsumer {
    //和队列相对应的 处理消费者队列的
    @Resource
    private RabbitTemplate rabbitTemplate;
    @Resource
    private PlatformAccountLogsMapper platformAccountLogsMapper;
    @Resource
    private PlatformAccountMapper platformAccountMapper;
    @Resource
    private PlatformAccountRoleMapper platformAccountRoleMapper;
    @Resource
    private PlatformRoleMapper platformRoleMapper;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private PlatformRoleResourceMapper platformRoleResourceMapper;
    @Resource
    private PlatformResourceMapper platformResourceMapper;
    @Resource
    private PlatformInterfaceMapper platformInterfaceMapper;

    @RabbitHandler
    @RabbitListener(queuesToDeclare = {@Queue(name = "platLogin")})
    public void platLogin(HashMap<String,Object> map){
        PlatformAccountLogs logs =(PlatformAccountLogs) map.get("platformAccountLogs");
        logs.setPlatformLoginState(StateData.USER_LOGS_ISNOT_LOGIN);
        platformAccountLogsMapper.insert(logs);
        //将管理员的角色存进redis
        PlatformAccountRole accountRole = platformAccountRoleMapper.selectById(logs.getPlatformAccountId());
        PlatformRole role = platformRoleMapper.selectById(accountRole.getPlatformRoleId());
        QueryWrapper<PlatformInterface> wrapper = new QueryWrapper<>();
        wrapper.eq("platform_role_id",role.getPlatformRoleId());
        List<PlatformInterface> platformInterfaces = platformInterfaceMapper.selectList(wrapper);
        Set<String> mySet = new HashSet<>();
        for (PlatformInterface platformInterface : platformInterfaces) {
            mySet.add(platformInterface.getPlatformInterfaceName());
        }
        redisUtil.sadd(RedisKey.platformRoleInterface(accountRole.getPlatformAccountId().toString()),mySet);
        //角色信息
        redisUtil.set(RedisKey.platformRole(accountRole.getPlatformAccountId().toString()),role.getPlatformRoleName());



    }
    @RabbitHandler
    @RabbitListener(queuesToDeclare = {@Queue(name = "upPlatformData")})
    public void upPlatformData(HashMap<String,Object> map){
        PlatformAccount account = (PlatformAccount) map.get("platformAccount");
        System.out.println("修改消息队列"+account);
        platformAccountMapper.updateById(account);
    }
    @RabbitHandler
    @RabbitListener(queuesToDeclare = {@Queue(name = "addPlatformData")})
    public void addPlatformData(HashMap<String,Object> map ){
        PlatformAccount account = (PlatformAccount) map.get("platformAccount");
        platformAccountMapper.updateById(account);
    }
    @RabbitHandler
    @RabbitListener(queuesToDeclare = {@Queue(name = "addResourceData")})
    public void addResourceData(HashMap<String,Object> map ){
        PlatformResource platformResource = (PlatformResource) map.get("platformResource");
        platformResourceMapper.updateById(platformResource);
    }
    @RabbitHandler
    @RabbitListener(queuesToDeclare = {@Queue(name = "upResourceData")})
    public void upResourceData(HashMap<String,Object> map ){
        PlatformResource platformResource = (PlatformResource) map.get("platformResource");
        platformResourceMapper.updateById(platformResource);
    }
    @RabbitHandler
    @RabbitListener(queuesToDeclare = {@Queue(name = "addPlatformRole")})
    public void addPlatformRole(HashMap<String,Object> map ){
        PlatformRole platformRole = (PlatformRole) map.get("platformRole");
        platformRoleMapper.updateById(platformRole);
    }
    @RabbitHandler
    @RabbitListener(queuesToDeclare = {@Queue(name = "upResourceInfo")})
    public void upResourceInfo(HashMap<String,Object> map ){
        PlatformResource resource = (PlatformResource) map.get("platformResource");
        platformResourceMapper.updateById(resource);
    }








}
